package com.qd.p2p.web.action;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import com.qdlc.p2p.biz.service.AccountRechargeService;
import com.qdlc.p2p.dal.model.AccountRechargeModel;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import com.qdlc.p2p.biz.service.BindingCardService;
import com.qdlc.p2p.biz.service.UserService;
import com.qdlc.p2p.biz.util.UserLoginUtil;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.constant.Constant;
import com.qdlc.p2p.common.core.ReturnResult;
import com.qdlc.p2p.dal.common.SystemConfigHelper;
import com.qdlc.p2p.dal.common.SystemConfigHelper.Nid;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.dto.UserIdentify;
import com.qdlc.p2p.dal.model.AccountBankModel;
import com.qdlc.p2p.dal.param.BindingCardReqestParam;
import com.qdlc.p2p.dal.util.UserTypeUtils;

/**
 * 绑定银行卡
 * @author linya 2016-01-06
 */
public class BindingCardAction extends BaseAction<Object>{

	@Resource
	UserService userService;
	
	@Resource
	BindingCardService bindingCardService;

	@Resource
	AccountRechargeService accountRechargeService;
	
	/**
	 * [绑卡]跳转到绑卡页面
	 */
	@Action(value="/member/cash/bank",results = { @Result(name = "newCash", type = "ftl", location = "/member/cash/bank.html"),
			@Result(name = "bank_firm", type = "ftl", location = "/member_borrow/cash/bank.html")})
	public String bank() throws Exception {
		// 获取当前的用户信息		
		UserIdentify userIdentify = UserLoginUtil.getUserIdentify();
		User user = userService.getCurrentSessionUser4Json(userIdentify);
		
		// 获取银行卡列表
		List<AccountBankModel> bankList = bindingCardService.findCardByUserId(user);
		request.setAttribute("bankList", bankList);

		AccountRechargeModel accountRecharge=accountRechargeService.getRechargeSummary(user.getUserId());
		if(accountRecharge.getRechargeTotal()<=0){
			request.setAttribute("unBund", true);
		}
		
		//判断用户是否绑定三方账号
		if (userIdentify != null && userIdentify.getMobilePhoneStatus() == 1) {
			request.setAttribute("result", true);
		}
		
		// 设置银行卡可以绑定卡数
		int bankNum = SystemConfigHelper.getIntegerValue(Nid.BANK_NUM);
		request.setAttribute("bankNum", bankNum);
		
		// 设置请求页面
		if (UserTypeUtils.isBorrowOrInverstment(user.getUserId())) {
			return "bank_firm";
		} 
		return "bank";
	}
	
	
	/**
	 * [添加银行卡] 跳转到添加银行卡页面
	 * @return
	 */
	@Action(value="/member/cash/addBankPage",results = {@Result(name = "result", type = "ftl", location = "member/tpp/"+Constant.TPP_PACKAGE+"/userBindCard.html")})
	public String addBank() throws Exception {
		return "result";
	}
	
    /**
     * [绑卡] 发送短信验证码
     * @throws Exception
     */
    @Action(value = "/tpp/tppSinaAction/sendBindBankCode")
    public void sendBindBankCode() throws Exception {
        User user = getSessionUser();
        BindingCardReqestParam bindingCardParam = populateBindingCardParam();
        ReturnResult result = bindingCardService.bindingBardForPhoneCode(user, bindingCardParam);
        if(result.isSuccess()){
        	printWebResult(result.getErrorInfo(), true, result.getExtra());
        }else{
        	printWebResult(result.getErrorInfo(), false);
        }
    }
    
    /**
     * [绑卡] 绑卡推进
     */
    @Action(value = "/tpp/tppSinaAction/tppBindBankCard")
    public void tppBindBankCard() throws Exception {
        User user = getSessionUser();
        String ticket = this.paramString("ticket");
        String phoneCode = this.paramString("code");
        BindingCardReqestParam bindingCardParam = populateBindingCardParam();
        ReturnResult result = bindingCardService.bindingBankCardAdvance(user, bindingCardParam, ticket, phoneCode);
        printWebResult(result.getErrorInfo(), result.isSuccess());
    }
    
    /**
     * 构建请求参数
     * @return
     */
    private BindingCardReqestParam populateBindingCardParam(){
    	long dictBankId = paramLong("dictBankId");
        String province = paramString("province");
        String city = paramString("city");
        String bankNo = paramString("bankNo");
        String mobilephone = paramString("mobilephone");
		String bankAddress = paramString("bankAddress");
    	BindingCardReqestParam bindingCardParam = new BindingCardReqestParam();
    	bindingCardParam.setDictBankId(dictBankId);
    	bindingCardParam.setBankNo(bankNo);
    	bindingCardParam.setMobilephone(mobilephone);
    	bindingCardParam.setProvince(province);
    	bindingCardParam.setCity(city);
		bindingCardParam.setBankAddress(bankAddress);
    	return bindingCardParam;
    }
    
	/**
	 * [解卡] 解绑/禁用银行卡
	 * @return
	 * @throws IOException
	 */
	@Action("/member/cash/disableBank")
	public void disableBank() throws Exception {
		// 获取当前的用户信息		
		UserIdentify userIdentify = UserLoginUtil.getUserIdentify();
		User user = userService.getCurrentSessionUser4Json(userIdentify);
		String bankNo = paramString("bankNo");
		String phoneCode = paramString("code");
		ReturnResult result = bindingCardService.disableBank(user, bankNo, phoneCode);
		printWebResult(result.getErrorInfo(), result.isSuccess());
	}

	/**
	 * [解绑卡] 发送短信验证码
	 * @throws Exception
	 */
	@Action(value = "/tpp/tppSinaAction/sendUnBindBankCode")
	public void sendUnBindBankCode() throws Exception {
		User user = getSessionUser();
		ReturnResult result = bindingCardService.disableBardForPhoneCode(user);
		if(result.isSuccess()){
			printWebResult(result.getErrorInfo(), true, result.getExtra());
		}else{
			printWebResult(result.getErrorInfo(), false);
		}
	}

	/**
	 * [解绑卡] 解绑推进
	 */
	@Action(value = "/tpp/tppSinaAction/disableBankForEnterprise")
	public void disableBankForEnterprise() throws Exception {
		User user = getSessionUser();
		String phoneCode = this.paramString("validCode");
		ReturnResult result = bindingCardService.disableBankForEnterprise(user, phoneCode);
		printWebResult(result.getErrorInfo(), result.isSuccess());
	}
	
}
